package com.redpack.quick.controller;

import com.redpack.quick.common.beans.Result;
import com.redpack.quick.common.beans.UserTaskBean;
import com.redpack.quick.consts.Constants;
import com.redpack.quick.datasource.entity.UserInfoEntity;
import com.redpack.quick.datasource.entity.UserTaskInfoEntity;
import com.redpack.quick.service.IUserInfo;
import com.redpack.quick.service.IUserTaskInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;

/**
 * Created by shuttle on 4/3/18.
 */
@RestController
@RequestMapping(Constants.BASE_URL + "/user/task")
@Api(description = "用户任务相关")
public class UserTaskController extends BaseController {

    @Autowired
    private IUserInfo userInfo;

    @Autowired
    private IUserTaskInfo userTaskInfo;

    @ApiOperation(value = "获取用户任务信息", notes = "获取用户任务信息", response = Result.class)
    @RequestMapping(value = "/task/getUserTaskInfo", method = { RequestMethod.POST}, produces = "application/json;charset=utf-8")
    public Result<Page<UserTaskBean>> getUserTaskInfo(@RequestParam("id") String uid,
                                                      @RequestParam("pageSize") int pageSize,
                                                      @RequestParam("pageNum") int pageNum){
        return successResult(userTaskInfo.findByOpenId(uid, pageSize, pageNum));
    }

    @ApiOperation(value = "保存用户任务信息", notes = "保存用户任务信息", response = Result.class)
    @RequestMapping(value = "/task/saveUserTaskInfo", method = { RequestMethod.POST}, produces = "application/json;charset=utf-8")
    public Result<UserTaskInfoEntity> saveUserTaskInfo(@RequestBody UserTaskInfoEntity entity){
        if(StringUtils.isBlank(entity.getStatus())){
            entity.setStatus("10");
        }
        return successResult(userTaskInfo.save(entity));
    }

    @ApiOperation(value = "是否已经存在任务且未完成", notes = "是否已经存在任务且未完成", response = Result.class)
    @RequestMapping(value = "/task/hasTaskAlready", method = { RequestMethod.POST}, produces = "application/json;charset=utf-8")
    public Result<String> hasTaskAlready(@RequestParam("uid") String uid,
                                         @RequestParam("taskId") String taskId){
        return successResult(userTaskInfo.findByTaskIdAndUid(uid, taskId));
    }


}
